package com.qifan.alb2b.action.back;

import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qifan.alb2b.domain.CarInfoDomain;
import com.qifan.alb2b.domain.CityInfoDomain;
import com.qifan.alb2b.service.CarInfoService;
import com.qifan.alb2b.service.CityInfoService;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.ID;
import com.qifan.alb2b.util.Page;

/**
 * 接送后台管理ajax的CURD
 * 
 * @author hc24
 * 
 */
@Controller
@RequestMapping("/admin/car")
public class CarManageController {
	private CarInfoService carInfoService;
	private CityInfoService cityInfoService;


	public void setCarInfoService(CarInfoService carInfoService) {
		this.carInfoService = carInfoService;
	}

	public void setCityInfoService(CityInfoService cityInfoService) {
		this.cityInfoService = cityInfoService;
	}

	@RequestMapping("/query.adm")
	@ResponseBody
	public Page query(DataGridModel dgm, CarInfoDomain cid) {
		// spring太给力了，可以自动装配两个对象 会自动的装返回的Map转换成JSON对象
		return carInfoService.query(dgm, cid);
	}

	@RequestMapping("/getCar.adm")
	@ResponseBody
	public CarInfoDomain getCarById(String cid) {
		return carInfoService.getCarById(cid);
	}

	@RequestMapping("/add.adm")
	@ResponseBody
	public Map<String, Object> add(CarInfoDomain carInfoDomain) {
		CityInfoDomain cityInfoDomain=cityInfoService.getCityById(carInfoDomain.getCityId());
		Map<String, Object> map = new HashMap<String, Object>();
		carInfoDomain.setCarId(ID.next() + "");// 设置唯一ID
		carInfoDomain.setCityCn(cityInfoDomain.getCityCn());
		carInfoService.add(carInfoDomain);
		map.put("success", true);
		return map;
	}

	@RequestMapping("/update.adm")
	@ResponseBody
	public Map<String, Object> update(CarInfoDomain carInfoDomain) {
		CityInfoDomain cityInfoDomain=cityInfoService.getCityById(carInfoDomain.getCityId());
		Map<String, Object> map = new HashMap<String, Object>();
		carInfoDomain.setCityCn(cityInfoDomain.getCityCn());
		carInfoService.update(carInfoDomain);
		map.put("success", true);
		return map;
	}

	@RequestMapping("/del.adm")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam("cid") String[] ids) {
		Map<String, Object> map = new HashMap<String, Object>();
		carInfoService.delete(ids);
		map.put("success", true);
		return map;
	}
}
